<template>
  <div id="userRegisterPage">
    <div class="icon">
      <img
        src="@/assets/loginIcon.jpg"
        alt="图标"
        class="owl-icon"
      >
    </div>
    <div class="title">
      <h1 class="app-name">花海图库</h1>
    </div>
    <div class="description">创建账号</div>
    <a-form
      :model="formState"
      name="login"
      autocomplete="off"
      @finish="handleSubmit"
    >
      <a-form-item
        name="userAccount"
        :rules="[
          { required: true, message: '用户账号不能为空!' },
          { min: 4, message: '用户账号长度不能小于4' },
        ]"
      >
        <a-input
          v-model:value="formState.userAccount"
          placeholder="请输入账号"
        >
          <template #prefix>
            <UserOutlined class="site-form-item-icon" />
          </template>
        </a-input>
      </a-form-item>

      <a-form-item
        name="userPassword"
        :rules="[
          { required: true, message: '密码不能为空!' },
          {
            min: 8,
            message: '密码长度不能小于8',
          },
        ]"
      >
        <a-input-password
          v-model:value="formState.userPassword"
          placeholder="请输入密码"
        >
          <template #prefix>
            <LockOutlined class="site-form-item-icon" />
          </template>
        </a-input-password>
      </a-form-item>

      <a-form-item
        name="checkPassword"
        :rules="[
          { required: true, message: '确认密码不能为空!' },
          {
            min: 8,
            message: '确认密码长度不能小于8',
          },
        ]"
      >
        <a-input-password
          v-model:value="formState.checkPassword"
          placeholder="请确认输入的密码"
        >
          <template #prefix>
            <CheckCircleOutlined class="site-form-item-icon" />
          </template>
        </a-input-password>
      </a-form-item>

      <a-form-item class="tips">
        <span>已有帐号？</span>
        <router-link to="/user/login" class="register-link">
          去登录
        </router-link>
      </a-form-item>

      <a-form-item class="submit">
        <a-button
          type="primary"
          html-type="submit"
          class="register-button"
        >
          注册
        </a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script lang="ts" setup>
import { reactive } from 'vue'
import { userLoginUsingPost, userRegisterUsingPost } from '@/api/userController'
import { useLoginUserStore } from '@/stores/userLogin'
import { message } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import { UserOutlined, LockOutlined, CheckCircleOutlined } from '@ant-design/icons-vue';

const router = useRouter();
const loginUserStore = useLoginUserStore();

const formState = reactive<API.UserRegisterDTO>({
  userAccount: '',
  userPassword: '',
  checkPassword: ''
})

const handleSubmit = async (values: any) => {
  // 校验两次输入的密码是否一致
  if(values.userPassword !== values.checkPassword){
    message.error("两次输入的密码不一致");
    return;
  }
  const res = await userRegisterUsingPost(values);
  if(res.data?.code === 0 && res.data.data){
    await loginUserStore.fetchLoginUser();
    message.success("注册成功");
    await router.push({
      path: '/user/login',
      replace: true,
    });
  }else {
    message.error("注册失败" + res.data?.message);
  }
}
</script>

<style scoped>
/*全局样式*/
#userRegisterPage {
  max-width: 360px;
  margin: 60px auto;
  padding: 30px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.1);
  text-align: center;
}

/*顶部icon*/
.icon{
  text-align: center;
  margin-top: 0;
  margin-bottom: 16px;
}

.owl-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
}

/*表单标题*/
.title {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 12px;
}

.app-name {
  font-size: 24px;
  font-weight: bold;
  color: #333;
}

/*表单描述*/
.description {
  font-size: 14px;
  color: #666;
  margin-bottom: 24px;
}

/*输入框icon样式*/
.site-form-item-icon {
  color: #1890ff;
}

/*注册tips*/
.tips {
  text-align: center;
  font-size: 14px;
  color: #999;
  margin-bottom: 16px;
}

.register-link {
  color: #1890ff;
  margin-left: 4px;
  text-decoration: none;
}

/*注册按钮*/
.register-button{
  width: 100%;
  background-color: #4CAF50;
  border-color: #4CAF50;
  box-shadow: 0 4px 12px rgba(76, 175, 80, 0.2);
}

</style>
